package com.kady.service.impl;

import com.kady.Dao.TableDao;
import com.kady.contant.BeanFactoryConstant;
import com.kady.contant.ExceptionConstant;
import com.kady.contant.MessageConstant;
import com.kady.entity.ResultVO;
import com.kady.entity.Table;
import com.kady.factory.BeanFactory;
import com.kady.service.TableService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/26
 * \* Time: 19:14
 * \* 千峰JAVA2101班
 * \* Description: 餐桌业务层Impl
 * \
 */
public class TableServiceImpl implements TableService {
    private TableDao tableDao = (TableDao) BeanFactory.getBean(BeanFactoryConstant.TABLE_DAO);

    /**
     * 获取餐桌列表
     */
    @Override
    public List<Table> getTableList(String keyword) throws SQLException {
        List<Table> res = tableDao.selectTableList("%" + (keyword==null?"":keyword.trim()) + "%");
        return res;
    }

    /**
     * 添加餐桌
     */
    @Override
    public void addTable(Table table) throws SQLException {
        tableDao.addTable(table.getTableName(), (byte) 0);
    }

    @Override
    public Object updateStatus(String tableId) throws SQLException {
        Table table = tableDao.selectTable(tableId);
        if(table==null){
            return new ResultVO(false, ExceptionConstant.NOT_FIND_TABLE_INFO);
        }
        /**
         * 判断原餐桌信息更改状态和时间
         */
        byte status = table.getStatus();
        table.setStatus((byte) (status==0?1:0));
        table.setBookedTime(status==0?new Date():null);
        /**
         * 将新的餐桌信息用来取修改数据库的餐桌信息
         */
        tableDao.update(table.getStatus(),table.getBookedTime(),table.getTableId());

        return null;
    }

    /**
     * 删除餐桌
     */
    @Override
    public void removeTable(String tableId) throws SQLException {
        tableDao.deleteTable(tableId);
    }

    /**
     * 查询餐桌
     */
    @Override
    public Table getTable(String tableId) throws SQLException {
        Table table = tableDao.selectTable(tableId);
        return table;
    }
}
